package cn.iune.javax.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendingMail {

	public static void main(String args[]) {
		try {
			/*
			send("lsp121043@126.com", "lsp121043@126.com", "sp2582144+",  
			        "lsp121043@126.com", "Java Mail 测试邮件"
			        , "<a>html 元素</a>：<b>邮件内容1</b>"
			        );
			*/
			sendHtml("postmaster@iune.cn", "postmaster@iune.cn", "Msid1635335",  
			        "lsp121043@126.com", "Java Mail 邮件"
			        , "<a>html 元素</a>：<b>邮件内容1</b>,您好！"
			        );
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void sendText(
			String fromMail, String username, String password,  
            String toMail,  String mailTitle, String mailContent
            ) throws AddressException, MessagingException {
		
		Properties properties = new Properties();
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
//		properties.put("mail.smtp.host", "smtp.126.com");
		properties.put("mail.smtp.host", "smtp.mxhichina.com");
		properties.put("mail.smtp.port", "25");
		
		Session session = Session.getDefaultInstance(properties);
		//session.setDebug(true); //有他会打印一些调试信息。
		  
		MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
		message.setFrom(new InternetAddress(fromMail));//设置发件人的地址
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));//设置收件人,并设置其接收类型为TO  
		message.setSubject(mailTitle);//设置标题
	    //设置信件内容
		message.setText(mailContent); //发送 纯文本 邮件 todo
//		message.setContent(mailContent, "text/html;charset=gbk"); //发送HTML邮件，内容样式比较丰富  
		message.setSentDate(new Date());//设置发信时间
		message.saveChanges();//存储邮件信息

		// Send the message by authenticating the SMTP server
		// Create a Transport instance and call the sendMessage
		Transport t = session.getTransport("smtp");
		try {
			// connect to the smpt server using transport
			// instance
			// change the user and password accordingly
		    //发送邮件
			Transport transport = session.getTransport("smtp");
//		    Transport transport = session.getTransport();
		    transport.connect(username, password);
		    transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址  
		    transport.close();
		} finally {
			t.close();
		}
		System.out.println("message sended successfully ....");

	}
	public static void sendHtml(
			String fromMail, String username, String password,  
            String toMail,  String mailTitle, String mailContent
            ) throws AddressException, MessagingException {
		
		Properties properties = new Properties();
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
//		properties.put("mail.smtp.host", "smtp.126.com");
		properties.put("mail.smtp.host", "smtp.mxhichina.com");
		properties.put("mail.smtp.port", "25");
		
		Session session = Session.getDefaultInstance(properties);
		//session.setDebug(true); //有他会打印一些调试信息。
		  
		MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
		message.setFrom(new InternetAddress(fromMail));//设置发件人的地址
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));//设置收件人,并设置其接收类型为TO  
		message.setSubject(mailTitle);//设置标题
	    //设置信件内容
		//message.setText(mailContent); //发送 纯文本 邮件 todo
		message.setContent(mailContent, "text/html;charset=gbk"); //发送HTML邮件，内容样式比较丰富  
		message.setSentDate(new Date());//设置发信时间
		message.saveChanges();//存储邮件信息

		// Send the message by authenticating the SMTP server
		// Create a Transport instance and call the sendMessage
		Transport t = session.getTransport("smtp");
		try {
			// connect to the smpt server using transport
			// instance
			// change the user and password accordingly
		    //发送邮件
			Transport transport = session.getTransport("smtp");
//		    Transport transport = session.getTransport();
		    transport.connect(username, password);
		    transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址  
		    transport.close();
		} finally {
			t.close();
		}
		System.out.println("message sended successfully ....");

	}


}
